package com.woniuxy.woniuhis.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.woniuxy.woniuhis.entity.SurgeryEmpArr;
import com.woniuxy.woniuhis.service.SurgeryEmpArrService;
import com.woniuxy.woniuhis.util.ResponseJson;
import com.woniuxy.woniuhis.util.UUIDUtil;

@Controller
@RequestMapping("/surgeryemparr")
public class SurgeryEmpArrController {
	
	@Autowired
	SurgeryEmpArrService  surgeryEmpArrService;
	
	// 添加手术人员安排
	@RequestMapping("/add")
	@ResponseBody
	public ResponseJson addSurgeryEmpArr(@RequestBody ResponseJson surgeryEmpArrJson) {
		ResponseJson responseJson = new ResponseJson();
		// jackson json串转对象
		ObjectMapper objectMapper = new ObjectMapper();
		try {
			// 先从数据库中查询手术室的人员安排，然后直接删除，然后重新插入吧
			List data = surgeryEmpArrJson.getData();
			String surgeryExeId = (String) ((Map)data.get(0)).get("surgeryExeId");
			List<SurgeryEmpArr> surgeryEmpArrInDb = surgeryEmpArrService.findSurgeryEmpArrBySurgeryId(surgeryExeId);
			if(!CollectionUtils.isEmpty(surgeryEmpArrInDb)) {
				for(SurgeryEmpArr sur:surgeryEmpArrInDb) {
					surgeryEmpArrService.delSurgerySurgeryEmpArrById(sur.getArrId());
				}
			}
			List<SurgeryEmpArr> empArrList = new ArrayList<SurgeryEmpArr>();
			for(Object arr:data) {
				SurgeryEmpArr surgeryEmpArr = objectMapper.convertValue(arr, SurgeryEmpArr.class);
				//System.out.println(surgeryEmpArr);
				surgeryEmpArr.setArrId(UUIDUtil.uuid32());
				empArrList.add(surgeryEmpArr);
			}
			surgeryEmpArrService.addSurgeryEmpArr(empArrList);
			responseJson.setCode(0);
			responseJson.setMsg("添加成功");
			return responseJson;
		} catch (Exception e) {
			responseJson.setCode(500);
			responseJson.setMsg(e.toString());
			e.printStackTrace();
			return responseJson;
		}
	}
	// 查询具体手术人员安排
	@RequestMapping("/get")
	@ResponseBody
	public ResponseJson getSurgeryAllEmpArr(String surgeryExeId) {
		ResponseJson responseJson = new ResponseJson();
		try {
			List<SurgeryEmpArr> surgeryEmpArr = surgeryEmpArrService.findSurgeryEmpArrBySurgeryId(surgeryExeId);
			responseJson.setCode(0);
			responseJson.setData(surgeryEmpArr);
			return responseJson;
		} catch (Exception e) {
			responseJson.setCode(500);
			responseJson.setMsg(e.toString());
			e.printStackTrace();
			return responseJson;
		}
	}
	
}
